<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="springboot,mongo," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！1、引入jarmaven &amp;lt;dependency&amp;gt;    &amp;lt;groupId&amp;gt;org.springframework.data&amp;lt;/groupId&amp;gt;">
<meta name="keywords" content="springboot,mongo">
<meta property="og:type" content="article">
<meta property="og:title" content="springboot对Mongo增删改查的方式">
<meta property="og:url" content="https://gitee.com/shuhao/2017/11/20/springboot对Mongo增删改查的方式/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！1、引入jarmaven &amp;lt;dependency&amp;gt;    &amp;lt;groupId&amp;gt;org.springframework.data&amp;lt;/groupId&amp;gt;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-11-22T07:59:18.060Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="springboot对Mongo增删改查的方式">
<meta name="twitter:description" content="MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！1、引入jarmaven &amp;lt;dependency&amp;gt;    &amp;lt;groupId&amp;gt;org.springframework.data&amp;lt;/groupId&amp;gt;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2017/11/20/springboot对Mongo增删改查的方式/"/>





  <title> springboot对Mongo增删改查的方式 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2017/11/20/springboot对Mongo增删改查的方式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                springboot对Mongo增删改查的方式
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-11-20T19:27:40+08:00">
                2017-11-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/springboot/" itemprop="url" rel="index">
                    <span itemprop="name">springboot</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！"><a href="#MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！" class="headerlink" title="MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！"></a>MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！</h1><h2 id="1、引入jar"><a href="#1、引入jar" class="headerlink" title="1、引入jar"></a>1、引入jar</h2><h3 id="maven"><a href="#maven" class="headerlink" title="maven"></a>maven</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> &lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>
<h3 id="gradle"><a href="#gradle" class="headerlink" title="gradle"></a>gradle</h3><figure class="highlight gradle"><table><tr><td class="code"><pre><span class="line"><span class="keyword">compile</span>(<span class="string">'org.springframework.boot:spring-boot-starter-data-mongodb'</span>)</span><br></pre></td></tr></table></figure>
<h2 id="2、DBObject方式"><a href="#2、DBObject方式" class="headerlink" title="2、DBObject方式"></a>2、DBObject方式</h2><p>下面就用DBObject的方式来实现对Mongo的操作<br>数据库为T_Fruit<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#123; &quot;_id&quot;: &quot;apples&quot;, &quot;qty&quot;: 5 &#125;</span><br><span class="line">&#123; &quot;_id&quot;: &quot;bananas&quot;, &quot;qty&quot;: 7 &#125;</span><br><span class="line">&#123; &quot;_id&quot;: &quot;oranges&quot;, &quot;qty&quot;: &#123; &quot;in stock&quot;: 8, &quot;ordered&quot;: 12 &#125; &#125;</span><br><span class="line">&#123; &quot;_id&quot;: &quot;avocados&quot;, &quot;qty&quot;: &quot;fourteen&quot; &#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="2-1-增"><a href="#2-1-增" class="headerlink" title="2.1 增"></a>2.1 增</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">DBObject fruit = <span class="keyword">new</span> DBObject();</span><br><span class="line">fruit.put(<span class="string">"_id"</span>:<span class="string">"watermelon"</span>);</span><br><span class="line">fruit.put(<span class="string">"qty"</span>:<span class="number">20</span>);</span><br><span class="line">mongoTemplate.getCollection(<span class="string">"T_Fruit"</span>).insert(fruit);</span><br></pre></td></tr></table></figure>
<h3 id="2-2-删"><a href="#2-2-删" class="headerlink" title="2.2 删"></a>2.2 删</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//删除_id为watermelon的文档</span></span><br><span class="line">DBObject del_query = <span class="keyword">new</span> BasicDBObject();</span><br><span class="line">del_query.put(<span class="string">"_id"</span>, <span class="string">"watermelon"</span>);</span><br><span class="line">mongoTemplate.getCollection(<span class="string">"T_Fruit"</span>).remove(del_query);</span><br></pre></td></tr></table></figure>
<h3 id="2-3-改"><a href="#2-3-改" class="headerlink" title="2.3 改"></a>2.3 改</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//把_id为apples的文档qty参数改为6</span></span><br><span class="line">DBObject query = <span class="keyword">new</span> BasicDBObject();</span><br><span class="line">query.append(<span class="string">"_id"</span>,<span class="string">"apples"</span>);</span><br><span class="line"></span><br><span class="line">BasicDBObject update = <span class="keyword">new</span> BasicDBObject();</span><br><span class="line">update.append(<span class="string">"$set"</span>,<span class="keyword">new</span> BasicDBObject(<span class="string">"qty"</span>,<span class="number">6</span>));</span><br><span class="line"></span><br><span class="line">mongoTemplate.getCollection(<span class="string">"T_Fruit"</span>).update(query,update);</span><br></pre></td></tr></table></figure>
<h3 id="3-4-查"><a href="#3-4-查" class="headerlink" title="3.4 查"></a>3.4 查</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//查询_id为apples的文档</span></span><br><span class="line">DBObject query = <span class="keyword">new</span> BasicDBObject() ;</span><br><span class="line">query.put(<span class="string">"_id"</span>:<span class="string">"apples"</span>) ; <span class="comment">//添加查询条件</span></span><br><span class="line">DBCursor cursor = mongoTemplate.getCollection(<span class="string">"T_Fruit"</span>).find(query) ;</span><br><span class="line"><span class="keyword">while</span>(cursor.hasNext())&#123;</span><br><span class="line">	DBObject fruit = cursor.next();</span><br><span class="line">	<span class="keyword">int</span> qty = (Integer)fruit.get(<span class="string">"qty"</span>) ;</span><br><span class="line">	System.out.println(<span class="string">"fruti apples qty is"</span>+qty) ;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//gt,in,or等查询</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//查询qty大于5的文档</span></span><br><span class="line">BasicDBObject gt_query = <span class="keyword">new</span> BasicDBObject()</span><br><span class="line">	.append(<span class="string">"qty"</span>, <span class="keyword">new</span> BasicDBObject().append(QueryOperators.GT, <span class="number">5</span>));</span><br><span class="line">DBCursor cursor = mongoTemplate.getCollection(<span class="string">"T_Fruit"</span>).find(gt_query) ;</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询_id 为apples,bananas的文档 ,只显示10个</span></span><br><span class="line">BasicDBList inList = <span class="keyword">new</span> BasicDBList() ;</span><br><span class="line">inList.add(<span class="keyword">new</span> BasicDBObject().append(<span class="string">"_id"</span>,<span class="string">"apples"</span>));</span><br><span class="line">inList.add(<span class="keyword">new</span> BasicDBObject().append(<span class="string">"_id"</span>,<span class="string">"bananas"</span>)) ;</span><br><span class="line">BasicDBObject in_query = <span class="keyword">new</span> BasicDBObject()</span><br><span class="line">	.append(QueryOperator.IN, inList) ;</span><br><span class="line">DBCursor cursor = mongoTemplate.getCollection(<span class="string">"T_Fruit"</span>).find(in_query).limit(<span class="number">10</span>) ;</span><br></pre></td></tr></table></figure>
<p>这种方式操作Mongo数据库最主要的接口为：DBObject。其实就是将Mongo的操作语句进行了java类封装。对于一些语句的参数，也有各种Operator类来封装。<br>获得的结果封装在游标DBCursor对象中</p>
<h2 id="3-对象方式操作Mongo"><a href="#3-对象方式操作Mongo" class="headerlink" title="3 对象方式操作Mongo"></a>3 对象方式操作Mongo</h2><h3 id="增"><a href="#增" class="headerlink" title="增"></a>增</h3><h3 id="删"><a href="#删" class="headerlink" title="删"></a>删</h3><h3 id="改"><a href="#改" class="headerlink" title="改"></a>改</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Update deleteKey = <span class="keyword">new</span> Update().unset(<span class="string">"key"</span>);</span><br><span class="line"></span><br><span class="line">      Update update1 = <span class="keyword">new</span> Update().set(<span class="string">"key"</span>,<span class="string">"value"</span>) ;</span><br><span class="line"></span><br><span class="line">      Update update2 = <span class="keyword">new</span> Update().addToSet(<span class="string">"key"</span>, <span class="string">"value"</span>) ;</span><br><span class="line"></span><br><span class="line">      mongoTemplate.updateFirst(query, update,Model.class);</span><br><span class="line">      mongoTemplate.updateMulti(query, update1,Model.class);</span><br></pre></td></tr></table></figure>
<h3 id="查"><a href="#查" class="headerlink" title="查"></a>查</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Criteria criteria1 = Criteria.where(<span class="string">"key"</span>).is(<span class="string">"value"</span>);</span><br><span class="line">Criteria criteria2 = Criteria.where(<span class="string">"key"</span>).exists(<span class="keyword">true</span>);</span><br><span class="line">Criteria criteria3 = Criteria.where(<span class="string">"key"</span>).in(<span class="string">""</span>,<span class="string">""</span>,<span class="string">""</span>);</span><br><span class="line">Criteria criteria4 = Criteria.where(<span class="string">"key"</span>).lt(<span class="number">2</span>);</span><br><span class="line">Criteria criteria5 = Criteria.where(<span class="string">"key"</span>).lte(<span class="number">1</span>);</span><br><span class="line">Criteria criteria6 = Criteria.where(<span class="string">"key"</span>).gt(<span class="number">3</span>);</span><br><span class="line">Criteria criteria7 = Criteria.where(<span class="string">"key"</span>).gte(<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">Criteria orCriteria = <span class="keyword">new</span> Criteria();</span><br><span class="line">orCriteria.orOperator(criteria1,criteria2);</span><br><span class="line"></span><br><span class="line">Criteria andCriteria = <span class="keyword">new</span> Criteria();</span><br><span class="line">andCriteria.andOperator(criteria3, criteria4);</span><br><span class="line"></span><br><span class="line">Query query = <span class="keyword">new</span> Query();</span><br><span class="line">query.addCriteria(criteria1) ;</span><br><span class="line"></span><br><span class="line">Query query1 = <span class="keyword">new</span> Query(criteria2);</span><br><span class="line"></span><br><span class="line">Query query2 = <span class="keyword">new</span> Query();</span><br><span class="line">query.addCriteria(orCriteria) ;</span><br><span class="line"></span><br><span class="line">Sort sort = <span class="keyword">new</span> Sort(<span class="keyword">new</span> Sort.Order(Sort.Direction.ASC,<span class="string">"key"</span>));</span><br><span class="line"></span><br><span class="line">query.with(sort);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> page = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">int</span> size = <span class="number">100</span>;</span><br><span class="line">Pageable pageable = <span class="keyword">new</span> PageRequest(page,size);</span><br><span class="line">query.with(pageable);</span><br><span class="line"></span><br><span class="line">mongoTemplate.find(query, Model.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//匹配</span></span><br><span class="line">MatchOperation matchOperation = Aggregation.match(Criteria.where(<span class="string">"key"</span>).is(<span class="string">"value"</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//排序</span></span><br><span class="line">SortOperation sortOperation = Aggregation.sort(<span class="keyword">new</span> Sort(Sort.Direction.ASC,<span class="string">"key"</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//跳过</span></span><br><span class="line"><span class="keyword">int</span> offset = <span class="number">10</span> ;</span><br><span class="line">SkipOperation skipOperation = Aggregation.skip(offset);</span><br><span class="line"></span><br><span class="line"><span class="comment">//限制</span></span><br><span class="line"><span class="keyword">int</span> limitset = <span class="number">100</span> ;</span><br><span class="line">LimitOperation limitOperation = Aggregation.limit(limitset);</span><br><span class="line"></span><br><span class="line"><span class="comment">//分组</span></span><br><span class="line">GroupOperation groupOperation = Aggregation.group(<span class="string">"key"</span>);</span><br><span class="line"></span><br><span class="line">TypedAggregation&lt;Model&gt; aggregation =  Aggregation.newAggregation(T.class, matchOperation,sortOperation,skipOperation,limitOperation, groupOperation);</span><br><span class="line"></span><br><span class="line"><span class="comment">//选项</span></span><br><span class="line">AggregationOptions aggregationOptions = <span class="keyword">new</span> AggregationOptions.Builder().allowDiskUse(<span class="keyword">true</span>).build();</span><br><span class="line"></span><br><span class="line">aggregation.withOptions(aggregationOptions);</span><br><span class="line"></span><br><span class="line">AggregationResults&lt;Model&gt; result = mongoTemplate.aggregate(aggregation, Model.class);</span><br><span class="line">List&lt;T&gt; data = result.getMappedResults();</span><br></pre></td></tr></table></figure>
<p>这种方式主要对象是Criteria， Query，Update等对象，并且将集合中的文档与java的实体类相互映射。避免了我们拿到数据后的封装转换。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/springboot/" rel="tag"># springboot</a>
          
            <a href="/blog/tags/mongo/" rel="tag"># mongo</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/11/20/springboot-配置mongo/" rel="next" title="springboot 配置mongo">
                <i class="fa fa-chevron-left"></i> springboot 配置mongo
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/11/22/SpringBoot中Environment的使用/" rel="prev" title="SpringBoot中Environment的使用">
                SpringBoot中Environment的使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！"><span class="nav-number">1.</span> <span class="nav-text">MongoDB是NoSql数据库，那么在项目中，我们如何操作MongoDB？SpringBoot提供了一个一个类似Hibernate功能的操作接口。今天来总结一下，如何在SpringBoot项目中操作Mongo！</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、引入jar"><span class="nav-number">1.1.</span> <span class="nav-text">1、引入jar</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#maven"><span class="nav-number">1.1.1.</span> <span class="nav-text">maven</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gradle"><span class="nav-number">1.1.2.</span> <span class="nav-text">gradle</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、DBObject方式"><span class="nav-number">1.2.</span> <span class="nav-text">2、DBObject方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-增"><span class="nav-number">1.2.1.</span> <span class="nav-text">2.1 增</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-删"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.2 删</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-改"><span class="nav-number">1.2.3.</span> <span class="nav-text">2.3 改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-查"><span class="nav-number">1.2.4.</span> <span class="nav-text">3.4 查</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-对象方式操作Mongo"><span class="nav-number">1.3.</span> <span class="nav-text">3 对象方式操作Mongo</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#增"><span class="nav-number">1.3.1.</span> <span class="nav-text">增</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删"><span class="nav-number">1.3.2.</span> <span class="nav-text">删</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#改"><span class="nav-number">1.3.3.</span> <span class="nav-text">改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查"><span class="nav-number">1.3.4.</span> <span class="nav-text">查</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
